<template>
  <section class="time">
    <el-time-picker
      v-model="value"
      :editable="false"
      :format="format"
      :value-format="valueformat"
      :clearable="clearable"
      :disabled="disabled"
      :placeholder="placeholder"
      :picker-options="pickeroptions"
      @change="handleChange"
    ></el-time-picker>
  </section>
</template>
<script>
import mixins from './../mixins'
export default {
  name: 'VTime',
  mixins: [mixins],
  props: {
    option: {
      type: Object,
      default: {}
    }
  },
  data () {
    return {
      value: ''
    }
  },
  computed: {
    format () {
      return this.option.format || 'HH:mm:ss'
    },
    valueformat () {
      return this.option.valueformat || 'HH:mm:ss'
    },
    rangeOpen () {
      return this.option.rangeOpen
    },
    rangeBefore () {
      return this.option.rangeBefore
    },
    rangeAfter () {
      return this.option.rangeAfter
    },
    pickeroptions () {
      return {
        selectableRange: (() => {
          if (this.rangeOpen) {
            const before = this.rangeBefore
            const after = this.rangeAfter
            return [`${before} - ${after}`]
          } else {
            return ['00:00:00 - 23:59:59']
          }
        })()
      }
    }
  },
  methods: {
    getValue () {
      return this.value
    },

    setValue (v) {
      this.$set(this, 'value', v)
    },

    setCheck () {
      let { value, required } = this
      let error = false, errorMsg = ''
      if (value !== undefined && value !== null) {
        value = value
      }
      if (required && !value) {
        error = true
        errorMsg = '必填选项不能为空'
      }
      this.error_ = error
      this.error_message_ = errorMsg
      this.fieldCheck()
    },

    handleChange (e) {
      this.setCheck()
      this.dispatch_event('filed-value-change', e)
    }
  }
}
</script>
<style lang="scss" scoped>
.time {
  /deep/ {
    .el-date-editor {
      width: 100%;
    }
  }
}
</style>